package com.patchmanage.back.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patchmanage.back.entity.FileLibrary;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IFileLibraryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-03-31
 */
@Api(value = " 文件库 controller", tags = {"文件库 Api"})
@RestController
@RequestMapping("/back/filelibrary")
public class FileLibraryController {
    @Autowired
    public IFileLibraryService fileLibraryService;


    /**
     * 获取资产列表
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "分页获取文件列表接口", notes = "获取文件列表接口")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result<IPage<FileLibrary>> list(@ApiIgnore @RequestBody Dict dict) {

        Integer pageNo = dict.getInt("pageNo");
        Integer pageSize = dict.getInt("pageSize");
        IPage<FileLibrary> page = new Page(pageNo, pageSize);

        Date publishingTime = dict.getDate("publishingTime");
        String fileName = dict.getStr("fileName");
        String fileNo = dict.getStr("fileNo");
        Integer type = dict.getInt("type");
        Integer adaptationOs = dict.getInt("adaptationOs");
        Integer fileSource = dict.getInt("fileSource");

        QueryWrapper<FileLibrary> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(fileName)){
            queryWrapper.eq("file_name",fileName);
        }
        if(StrUtil.isNotBlank(fileName)){
            queryWrapper.eq("file_no",fileNo);
        }
        if(type != null){
            queryWrapper.eq("type",type);
        }
        if(adaptationOs != null){
            queryWrapper.eq("adaptation_os",adaptationOs);
        }
        if(fileSource != null){
            queryWrapper.eq("file_source",fileSource);
        }
        if(publishingTime != null){
            Date statTime = DateUtil.beginOfDay(publishingTime);
            Date endTime = DateUtil.endOfDay(publishingTime);

            queryWrapper.between("publishing_time",statTime,endTime);
        }

        return Result.success(fileLibraryService.page(page, queryWrapper));
    }

    /**
     * 获取资产列表 前10条
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取文件列表接口 前10条", notes = "获取文件列表接口 前10条")
    @RequestMapping(value = "listWithoutPage", method = RequestMethod.POST)
    public Result<Collection> listWithoutPage(@ApiIgnore @RequestBody Dict dict) {
        String fileName = dict.getStr("fileName");
        QueryWrapper<FileLibrary> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(fileName)){
            queryWrapper.likeRight("file_name",fileName);
        }
        queryWrapper.last("limit 10");
        return Result.success(fileLibraryService.list(queryWrapper));
    }

    /**
     * 获取文件信息
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取文件信息接口", notes = "获取文件信息接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "body"),
    })
    public Result<FileLibrary> getById(@ApiIgnore @RequestBody Dict dict) {
        return Result.success(fileLibraryService.getById(dict.getInt("id")));
    }


    /**
     * 新增或修改文件信息
     * @param fileLibrary
     * @return
     */
    @ApiOperation(value = "新增或修改文件信息接口", notes = "新增或修改文件信息接口")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    public Result<Boolean> saveOrUpdate(@RequestBody FileLibrary fileLibrary) {

        if(fileLibrary.getId() == null){
            Snowflake snowflake = IdUtil.getSnowflake(1, 1);
            long id = snowflake.nextId();
            fileLibrary.setFileNo("WJ" + id);
        }

        if(StrUtil.isNotBlank(fileLibrary.getSourceFileName())){
            String filename = fileLibrary.getSourceFileName();
            int location = filename.indexOf(".");
            String caselsh = "";
            if(location != -1){
                caselsh = filename.substring(filename.indexOf(".") + 1,filename.length());
                fileLibrary.setType(caselsh);
            }
        }

        return Result.success(fileLibraryService.saveOrUpdate(fileLibrary));
    }

    /**
     * 删除资产
     * @param dict
     * @return
     */
    @ApiOperation(value = "删除文件接口", notes = "删除文件接口")
    @RequestMapping(value = "removeById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "body"),
    })
    public Result<Boolean> removeById(@ApiIgnore @RequestBody Dict dict) {
        String id = dict.getStr("id");
        String[] ids = id.split(",");
        return Result.success(fileLibraryService.removeByIds( Arrays.asList(ids)));
    }
}
